package org.jxkj.utils;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.jxkj.util.PropertiesUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;


public class DruidUtils {
    private static DruidUtils druidUtil;
    private DataSource dataSource = null;
    private Connection conn = null;

    private DruidUtils() {
        try {
            Properties properties = PropertiesUtils.initial("druid.properties");
            dataSource = DruidDataSourceFactory.createDataSource(properties);
            conn = dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized static DruidUtils newInstance() {
        if (druidUtil == null) {
            druidUtil = new DruidUtils();
        }
        return druidUtil;
    }

    public Connection getConnection() throws SQLException {
        return conn;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void close() {
        try {
            conn.close();
        } catch (Exception e) {

        }
    }
}
